package com.scjp_Java6.String;

import java.io.File;



import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class ReadXML {
	public static void main(String[] args) {

//		try {
//
//			File file = new File("UserInfo.xml");
//
//			if (file.createNewFile()) {
//				System.out.println("File is created!");
//			} else {
//				System.out.println("File already exists.");
//			}
//
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	
		try {
			File fXmlFile=new File("UserInfo.xml");
			DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
			Document doc=dBuilder.parse(fXmlFile);
			
			doc.getDocumentElement().normalize();
			
			System.out.println("Root element: "+doc.getDocumentElement().getNodeName());
			
			NodeList nList=doc.getElementsByTagName("userprofile");
			System.out.println("------------------");
			
			for(int temp=0;temp<nList.getLength();temp++){
				Node nNode=nList.item(temp);
				System.out.println("\nCurrent Element: "+nNode.getNodeName());
				
//				if(nNode.getNodeType()==Node.ELEMENT_NODE){
					Element eElement=(Element) nNode;
//					System.out.println("User Profile: "+eElement.getElementsByTagName("userprofile").item(0).getTextContent());
					System.out.println("User Name: "+eElement.getElementsByTagName("username").item(0).getTextContent());
					System.out.println("Character Name: "+eElement.getElementsByTagName("charactername").item(0).getTextContent());
					System.out.println("Look: "+eElement.getElementsByTagName("look").item(0).getTextContent());
					System.out.println("Chloting: "+eElement.getElementsByTagName("clothing").item(0).getTextContent());
					System.out.println("Hair: "+eElement.getElementsByTagName("hair").item(0).getTextContent());
					System.out.println("Strength: "+eElement.getElementsByTagName("strength").item(0).getTextContent());
					System.out.println("Items: "+eElement.getElementsByTagName("items").item(0).getTextContent());
					System.out.println("Date: "+eElement.getElementsByTagName("date").item(0).getTextContent());
					
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}
